package final2107.day04;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ConsumerBlocking {
    public static void main(String[] args) {
        //创建阻塞队列
        BlockingQueue<String> queue = new
                LinkedBlockingQueue<>();
        //创建生产者线程
        Thread produtor = new Thread(){
            @Override
            public void run() {
                //每隔2s产生数据存入队列
                for (int i=0;i<100;i++){
                    try {
                        Thread.sleep(2000);
                        queue.put("hello"+i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };


        //创建消费者线程
        Thread consumer = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        String ele = queue.take();
                        System.out.println("取出:"+ele);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        produtor.start();
        consumer.start();


    }
}
